const app = getApp();
import {
  getMarketList,
  delMarketRecord,
  getNavBanner
} from "../../../config/api";
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 1,
    list: [],
    page: 1,
    limit: 10,
    isEmpty: false,
    banner: null, //顶部图片
  },


  getList() {
    let that = this;
    let params = {
      page: that.data.page,
      limit: that.data.limit,
      idcard: wx.getStorageSync('member').idcard ? wx.getStorageSync('member').idcard : null,
    };
    util.apiRequest(getMarketList, params, null).then(r => {
      console.log('获得的集市列表为：', r.data.data);
      that.setData({
        list: [...that.data.list, ...r.data.data]
      });
      if (r.data.data.length == 0 && that.data.page == 1) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }

      if (r.data.data.length == 0 && that.data.page != 1) {
        wx.showToast({
          title: '已经到底了',
          icon: 'loading',
          duration: 1000
        })
      }


    });
  }, //获得集市列表

  //跳转到相应的详情页面
  toMarketDetail(e) {
    let that = this;
    console.log('跳转到相应的详情页面：');
    wx.navigateTo({
      url: '/pages/new_index/market_detail/market_detail?id=' + e.currentTarget.dataset.id
    });
  },

  //预览图片
  previewImg(e) {
    let that = this;
    console.log('e.currentTarget.dataset.img:', e.currentTarget.dataset);
    wx.previewImage({
      current: e.currentTarget.dataset.img,
      urls: e.currentTarget.dataset.imgs
    });
  },

  //拨打电话
  dial(e) {
    let that = this;
    console.log('e', e.currentTarget.dataset.phone);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
    })
  },

  //删除记录
  delRecord(e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '您确认删除该记录吗？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          util.apiRequest(delMarketRecord, {
            id: item.id
          }, null).then(r => {
            console.log('删除结果为：', r.data.info);
            wx.showToast({
              title: r.data.info,
              icon: 'loading',
              duration: 2000
            })
            that.getMarketList();
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let model = wx.getStorageSync('model')
    if (model) {
      this.setData({
        model
      })
    }
    this.getList();
    if (options.item) {
      console.log('options.item:', JSON.parse(options.item));
      let item = JSON.parse(options.item);
      options.item ? this.getBanner(item.id) : console.log('没有id不用调用顶部图片');
    }

  },


  //获得顶部图片
  getBanner(id) {
    let that = this;
    console.log('调用顶部图片的栏目id是：', id);
    util.apiRequest(getNavBanner, {
      id: id
    }, null).then(r => {
      console.log('获得顶部栏目图片为：', r);
      that.setData({
        banner: r.data.data ? r.data.data.banner : null
      });
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    console.log('监听页面触底事件！');
    that.setData({
      page: that.data.page + 1
    });
    that.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})